﻿namespace ImageWallControl
{
    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Animation;

    public static class Animate
    {
        public static void AnimateBrush(DependencyObject target, Storyboard sb, Duration duration, float totalTime,
                                        Brush brush)
        {
            var anim = new ObjectAnimationUsingKeyFrames {Duration = duration};

            anim.KeyFrames.Add(new DiscreteObjectKeyFrame
                                   {
                                       Value = brush,
                                       KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalTime/2))
                                   });

            sb.Children.Add(anim);

            Storyboard.SetTarget(anim, target);

            Storyboard.SetTargetProperty(anim,
                                         new PropertyPath("Fill"));
        }

        public static void AnimatedBlur(DependencyObject target, Storyboard sb, Duration duration, float totalTime)
        {
            var blur = new DoubleAnimationUsingKeyFrames {Duration = duration};

            blur.KeyFrames.Add(new EasingDoubleKeyFrame
                                   {
                                       Value = 5,
                                       KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalTime/2)),
                                       EasingFunction = new QuarticEase {EasingMode = EasingMode.EaseIn}
                                   });
            blur.KeyFrames.Add(new EasingDoubleKeyFrame
                                   {
                                       Value = 0,
                                       KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalTime)),
                                       EasingFunction = new QuarticEase {EasingMode = EasingMode.EaseOut}
                                   });

            sb.Children.Add(blur);

            Storyboard.SetTarget(blur, target);

            Storyboard.SetTargetProperty(blur,
                                         new PropertyPath("(UIElement.Effect).(BlurEffect.Radius)"));
        }

        public static void AnimateScaleY(DependencyObject target, Storyboard sb, Duration duration, float totalTime)
        {
            var scaleY = new DoubleAnimationUsingKeyFrames {Duration = duration};

            scaleY.KeyFrames.Add(new EasingDoubleKeyFrame
                                     {
                                         Value = 0.4,
                                         KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalTime/2)),
                                         EasingFunction = new QuinticEase {EasingMode = EasingMode.EaseInOut}
                                     });
            scaleY.KeyFrames.Add(new EasingDoubleKeyFrame
                                     {
                                         Value = 1,
                                         KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalTime)),
                                         EasingFunction = new QuinticEase {EasingMode = EasingMode.EaseInOut}
                                     });

            sb.Children.Add(scaleY);

            Storyboard.SetTarget(scaleY, target);
            Storyboard.SetTargetProperty(scaleY,
                                         new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.ScaleY)"));
        }

        public static void AnimateScaleX(DependencyObject target, Storyboard sb, Duration duration, double totalTime)
        {
            var scaleX = new DoubleAnimationUsingKeyFrames {Duration = duration};

            scaleX.KeyFrames.Add(new EasingDoubleKeyFrame
                                     {
                                         Value = 0,
                                         KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalTime/2)),
                                         EasingFunction = new QuarticEase {EasingMode = EasingMode.EaseIn}
                                     });
            scaleX.KeyFrames.Add(new EasingDoubleKeyFrame
                                     {
                                         Value = 1,
                                         KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(totalTime)),
                                         EasingFunction = new QuarticEase {EasingMode = EasingMode.EaseOut}
                                     });

            sb.Children.Add(scaleX);

            Storyboard.SetTarget(scaleX, target);

            Storyboard.SetTargetProperty(scaleX,
                                         new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.ScaleX)"));
        }
    }
}